package com.wn.web;

import com.github.pagehelper.PageInfo;
import com.wn.annotation.RequireRoles;
import com.wn.auth.JWTUtil;
import com.wn.common.Const;
import com.wn.ov.DraRecordOv;
import com.wn.ov.UserOv;
import com.wn.pojo.DraRecord;
import com.wn.common.PageParam;
import com.wn.pojo.User;
import com.wn.resopnse.ServerResponse;
import com.wn.service.DraRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("draRecord")
public class DraRecordController {
    @Autowired
    private DraRecordService draRecordService;

    //退药申请记录
    @PostMapping("addDraRecord")
    @RequireRoles(roles = "medical")
    public ServerResponse addDraRecord(@RequestHeader("token") String token, @RequestBody @Validated DraRecordOv draRecordOv){
        System.out.println(draRecordOv);
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
            //调用添加方法
            ServerResponse serverResponse = draRecordService.addDraRecord(draRecordOv,userOv);
            return serverResponse;
    }

    //确认申请记录
    @PostMapping("confirmDraRecord")
    @RequireRoles(roles = "doctor,main_doctor")
    public ServerResponse confirmDraRecord(@RequestHeader("token") String token,@RequestBody DraRecord draRecord){
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        //确认人的id
        draRecord.setUpdateUserId(userOv.getId());
        ServerResponse serverResponse = draRecordService.confirmDraRecord(draRecord);
        return serverResponse;
    }

    //取消申请记录
    @PostMapping("cancelDraRecord")
    @RequireRoles(roles = "doctor,main_doctor")
    public ServerResponse cancelDraRecord(@RequestHeader("token") String token,@RequestBody DraRecord draRecord){
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        //取消人的id
        draRecord.setUpdateUserId(userOv.getId());
        ServerResponse serverResponse = draRecordService.cancelDraRecord(draRecord);
        return serverResponse;
    }

    //删除记录
    @PostMapping("deleteDraRecord")
    @RequireRoles(roles = "medical")
    public ServerResponse deleteDraRecord(@RequestHeader("token") String token,@RequestBody DraRecord draRecord){
        ServerResponse serverResponse = draRecordService.deleteDraRecord(draRecord);
        return serverResponse;
    }

    //查看退药记录
    @PostMapping("getAllDraRecord")
    @RequireRoles(roles = "medical,doctor,main_doctor")
    public ServerResponse getAllDraRecord(@RequestHeader("token")String token,@RequestBody PageParam<DraRecord> pageParam){
        return draRecordService.getAllDraRecord(pageParam);
    }

}
